package cn.manyufun.myf.ui.main.model;

import org.json.JSONException;
import org.json.JSONObject;

import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.bean.SearchBean;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.commonutils.APIUtils;
import cn.manyufun.myf.common.commonutils.DisplayUtil;
import cn.manyufun.myf.ui.main.contract.SearchContract;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import rx.Observable;

/**
 * Created by Xiao on 2018-02-03.
 */

public class SearchModel implements SearchContract.Model {
    @Override
    public Observable<SearchBean> search(int pageIndex, int pageSize, String keyword) {
        JSONObject root = new JSONObject();
        JSONObject head = APIUtils.getHeadParamter();
        try {
            root.put("Head", head);
            root.put("ImageSize", DisplayUtil.dip2px(91f) + "*" + DisplayUtil.dip2px(127f));
            root.put("PageIndex", pageIndex);
            root.put("PageSize", pageSize);
            root.put("Keyword", keyword);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json"), root.toString());
        return Api.getDefault(HostType.manyufun).search(requestBody)
                .compose(RxSchedulers.<SearchBean>io_main());

    }
}
